﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Foulds.Calculator.StateMachine
{
    #region Key Type
    /// <summary>
    /// The types of keys available on the calcualtor.
    /// </summary>
    public enum KeyType
    {
        None = 0,
        Operand,
        Operator,
        Function
    }
    #endregion

    #region Key Name
    /// <summary>
    /// The names of the keys that can appear on the calculator.
    /// </summary>
    public enum KeyName
    {
        Number0 = 0,
        Number1,
        Number2,
        Number3,
        Number4,
        Number5,
        Number6,
        Number7,
        Number8,
        Number9,
        Plus,
        Minus,
        Multiply,
        Divide,
        Equals,
        Backspace,
        Clear,
        MemoryClear,
        MemoryRecall,
        MemorySubtract,
        Inverse,
        Point,
        Reciprocal,
        Percentage,
        SquareRoot
    }
    #endregion

    #region Calculator Key
    /// <summary>
    /// This class represents a calcualtor key.
    /// </summary>
    public class CalculatorKey
    {
        #region Key Definitions
        /// <summary>
        /// The definition of all keys on the calculator
        /// </summary>
        private readonly static Dictionary<KeyName, KeyType> KeyDefinitions = new Dictionary<KeyName, KeyType> {
            { KeyName.Number0, KeyType.Operand},
            { KeyName.Number1, KeyType.Operand},
            { KeyName.Number2, KeyType.Operand},
            { KeyName.Number3, KeyType.Operand},
            { KeyName.Number4, KeyType.Operand},
            { KeyName.Number5, KeyType.Operand},
            { KeyName.Number6, KeyType.Operand},
            { KeyName.Number7, KeyType.Operand},
            { KeyName.Number8, KeyType.Operand},
            { KeyName.Number9, KeyType.Operand},
            { KeyName.Point, KeyType.Operand},
            { KeyName.Plus, KeyType.Operator},
            { KeyName.Backspace, KeyType.Function}
        };
        #endregion

        #region Properties
        /// <summary>The name of the calculator key.</summary>
        public KeyName Name { get; private set; }

        /// <summary>The type of calculator key.</summary>
        public KeyType Type { get; private set; }
        #endregion

        #region Constructor
        /// <summary>
        /// Create a new instance of the class.
        /// </summary>
        /// <param name="name">The name of the key.</param>
        public CalculatorKey(KeyName name)
        {
            // ensure that the key definition exist
            if (!KeyDefinitions.Keys.Contains(name))
                throw new ApplicationException("The calculator key is invalid.");

            // set the key properties
            this.Name = name;
            this.Type = KeyDefinitions[name];
        }
        #endregion
    }
    #endregion
}
